<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>hello react</title>
    <style></style>
  </head>
  <body>
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script src="../js/react.development.js"></script>
    <!-- 引入react Dom库 用于支持 react操作dom-->
    <script src="../js/react-dom.development.js"></script>
    <!-- 引入babel 用于将 jsx 转为 js -->
    <script src="../js/babel.min.js"></script>

    <!-- 使用babel 转译jsx -->
    <script type="text/babel">
      // 挂载 mount
      // 卸载 unmount
      // 1.创建组件
      class Life extends React.Component {
        state = { opacity: 0.5 };
        death = () => {
          // 卸载组件
          ReactDOM.unmountComponentAtNode(document.getElementById("test"));
        };
        // 生命周期，组件完成挂载，只调用一次
        componentDidMount() {
          this.timer = setInterval(() => {
            let { opacity } = this.state;
            opacity -= 0.1;
            if (opacity <= 0) opacity = 1;
            this.setState({ opacity });
          }, 200);
        }
        // 组件将要卸载
        componentWillUnmount() {
          clearInterval(this.timer);
        }
        // render调用的时机：初始化渲染，状态更新之后
        render() {
          return (
            <div>
              <h1 style={{ opacity: this.state.opacity }}>引出组件生命周期</h1>
              <button onClick={this.death}>点击</button>
            </div>
          );
        }
      }
      // 2.挂载组件到页面
      ReactDOM.render(<Life />, document.getElementById("test"));
    </script>
  </body>
</html>
